#include "menu.hpp"
#include <iostream>

Menu::Menu (void)
{
}

int Menu::Run (sf::RenderWindow &App)
{
    sf::Event Event;
    bool Running = true;
    sf::Texture texture;
    sf::Sprite Sprite;
    sf::Font Font;
    sf::Text play("Play");
    sf::Text exit("Exit");
    sf::Text options("Options");
    int menu = 0;

    if (!texture.loadFromFile("../images/starfield.png"))
    {
        std::cerr<<"Error loading starfield"<<std::endl;
       return (-1);
    }
    Sprite.setTexture(texture);
    if (!Font.loadFromFile("../images/DejaVuSans-Bold.ttf"))
    {
       std::cerr<<"Error loading font"<<std::endl;
        return (-1);
    }
    play.setFont(Font);
    play.setCharacterSize(20);
    play.setPosition(280,160);
    options.setFont(Font);
    options.setCharacterSize(20);
    options.setPosition(280,220);
    exit.setFont(Font);
    exit.setCharacterSize(20);
    exit.setPosition(280,280);


    //Clearing screen
    App.clear(sf::Color(0, 0, 0, 0));

    while (Running)
    {
        //Verifying events
        while (App.pollEvent(Event))
        {
            // Window closed
            if (Event.type == sf::Event::Closed)
            {
                return (-1);
            }
            //Key pressed
            if (Event.type == sf::Event::KeyPressed)
            {
                switch (Event.key.code)
                {
                    case sf::Keyboard::Up:
                        menu = (menu - 1)%3;
                        break;
                    case sf::Keyboard::Down:
                        menu = (menu + 1)%3;
                        break;
                    case sf::Keyboard::Return:
                        if (menu==0)
                        {
                            //Let's get play !
                            return (1);
                        }
                        else if(menu==2)
                        {
                            //Let's get work...
                            return (-1);
                        }
                        else{}
                        break;
                    default :
                        break;
                }
            }
        }
        //When getting at alpha_max, we stop modifying the sprite
        //if (alpha<alpha_max)
        //{
        //    alpha++;
        //}
        //Sprite.SetColor(sf::Color(255, 255, 255, alpha/alpha_div));
        if (menu==0)
        {
            play.setColor(sf::Color(0, 255, 0, 255));
            options.setColor(sf::Color(255, 255, 255, 255));
            exit.setColor(sf::Color(255, 255, 255, 255));

        }
        if (menu==1)
        {
            play.setColor(sf::Color(255, 255, 255, 255));
            options.setColor(sf::Color(0, 255, 0, 255));
            exit.setColor(sf::Color(255, 255, 255, 255));
        }
        if (menu==2)
        {
            play.setColor(sf::Color(255, 255, 255, 255));
            options.setColor(sf::Color(255, 255, 255, 255));
            exit.setColor(sf::Color(0, 255, 0, 255));

        }

        //Drawing
        //App.Draw(Sprite);

    App.draw(play);
    App.draw(options);
    App.draw(exit);
    App.display();
    }
    //Never reaching this point normally, but just in case, exit the application
    return (-1);
}
